package com.vimhe.easyimport.filter;

import com.vimhe.easyimport.config.bean.CellConfig;
import com.vimhe.easyimport.util.ValidateUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 默认的数据过滤器实现
 *
 * @author Vimhe
 */
public class DefaultDataFilter implements DataFilter {

    @Override
    public Object filter(CellConfig cellConfig, Object cellValue) {
        if (ValidateUtils.isEmpty(cellValue)) {
            return cellValue;
        }

        // 把时间类型的数据转换为指定的格式“yyyy-MM-dd”
        String type = cellConfig.getType();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        switch (type) {
            case "java.util.Date":
                try {
                    cellValue = simpleDateFormat.parse((String) cellValue);
                } catch (Exception ignored) {
                }
                break;
            case "java.sql.Date":
                try {
                    Date sqlDate = simpleDateFormat.parse((String) cellValue);
                    cellValue = new java.sql.Date(sqlDate.getTime());
                } catch (Exception ignored) {
                }
                break;
            case "java.sql.Timestamp":
                try {
                    cellValue = simpleDateFormat.parse((String) cellValue).getTime();
                } catch (Exception ignored) {
                }
                break;
            default:
                break;
        }
        return cellValue;
    }

}
